package com.haoxin.ai_course.modules.course.biz;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.haoxin.ai_course.common.utils.HttpJsonResult;
import com.haoxin.ai_course.modules.course.entity.AcCourse;
import com.haoxin.ai_course.modules.course.entity.AcCourseNode;
import com.haoxin.ai_course.modules.course.entity.AcCourseUser;
import com.haoxin.ai_course.modules.course.entity.AcNodeUser;
import com.haoxin.ai_course.modules.course.entity.AcStatisticsCourse;
import com.haoxin.ai_course.modules.course.entity.AcStatisticsCourseDetail;
import com.haoxin.ai_course.modules.course.entity.AcStatisticsUser;
import com.haoxin.ai_course.modules.course.entity.VO.AcNodeUserVO;
import com.haoxin.ai_course.modules.course.service.AcCourseNodeService;
import com.haoxin.ai_course.modules.course.service.AcCourseService;
import com.haoxin.ai_course.modules.course.service.AcCourseUserService;
import com.haoxin.ai_course.modules.course.service.AcNodeUserService;
import com.haoxin.ai_course.modules.course.service.AcStatisticsCourseDetailService;
import com.haoxin.ai_course.modules.course.service.AcStatisticsCourseService;
import com.haoxin.ai_course.modules.course.service.AcStatisticsUserService;
import com.haoxin.ai_course.modules.sys.entity.SysUser;
import com.haoxin.ai_course.modules.sys.service.SysUserService;

import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;

/**
 * 课程环节管理
 * 
 * @author xingxiangguo
 * @email bugcn@qq.com
 * @date 2022-06-06 09:44:49
 */
@Slf4j
@Component
public class AcNodeUserBusiness {
	@Autowired
	private MapperFacade mapperFacade;
	@Autowired
	 private   AcNodeUserService acNodeUserService;
	@Autowired
	  private   AcCourseNodeService acCourseNodeService;
	@Autowired
	 AcStatisticsCourseDetailService acStatisticsCourseDetailService;
	
	@Autowired
	AcCourseService acCourseService; 
	@Autowired
	 AcStatisticsUserService acStatisticsUserService;
	
	@Autowired
	AcStatisticsCourseService acStatisticsCourseService;
	@Autowired
	AcCourseUserService acCourseUserService;
	@Autowired
	SysUserService sysUserService ;
	
//	**
	/**
	 * 此处增加进入环节的人数统计
	 * @param courseId
	 * @param userId
	 * @return
	 */
	@Transactional
	public HttpJsonResult<List<AcNodeUserVO>> getListByCourseId(Integer courseId, Long userId,String mobile) {
		log.info("getListByCourseId 开始--------------");
		Long startTime = System.currentTimeMillis();
		log.info("getListByCourseId 入参：courseId:{},userId:{},mobile:{}",courseId,userId,mobile);
		//查询列表数据
    	HttpJsonResult<List<AcNodeUserVO>>  result = new HttpJsonResult<List<AcNodeUserVO>> ();
    	if(userId == null) {
			log.error("获取课程环节失败，原因，用户id为空");
			result.setMsg("获取课程环节失败，原因，用户id为空");
			return result;
		}
    	QueryWrapper< AcNodeUser> qw1 = new QueryWrapper<>();
    	qw1.lambda().eq(AcNodeUser::getCourseId, courseId);
    	qw1.lambda().eq(AcNodeUser::getUserId, userId);
    	qw1.lambda().orderByAsc(AcNodeUser::getNodeId);
    	List<AcNodeUser> acNodeUserList = acNodeUserService.list(qw1);
    	Long time1 = System.currentTimeMillis();
    	log.info("getListByCourseId 获取用户课程节点--------------，用时："+(System.currentTimeMillis()-time1));
    	List<AcNodeUserVO> voList = new ArrayList<>();
    	QueryWrapper<AcCourseUser> qw = new QueryWrapper<AcCourseUser>();
    	qw.lambda().eq(AcCourseUser::getUserId,userId);
    	qw.lambda().eq(AcCourseUser::getCourseId, courseId);
    	AcCourseUser acCourseUser = 	acCourseUserService.getOne(qw);
    	Long time2 = System.currentTimeMillis();
    	log.info("getListByCourseId 获取用户课程--------------，用时："+(System.currentTimeMillis()-time2));
    	AcNodeUserVO firstAcNodeUserVO = null;
    	if(CollectionUtils.isNotEmpty(acNodeUserList)) {
//    		改造提速
    		List<Integer> ids = acNodeUserList.stream().map(x->x.getNodeId()).collect(Collectors.toList());
    		QueryWrapper< AcCourseNode> qw3 = new QueryWrapper<AcCourseNode>();
    		qw3.lambda().eq(AcCourseNode::getCourseId, courseId);
    		qw3.lambda().in(AcCourseNode::getId, ids);
    		List<AcCourseNode>list = acCourseNodeService.list(qw3);
    		for (AcNodeUser acNodeUser : acNodeUserList) {
    			AcNodeUserVO acNodeUserVO = mapperFacade.map(acNodeUser, AcNodeUserVO.class);
    			List<AcCourseNode>list1 = 	list.stream().filter(x->x.getId().intValue() == acNodeUser.getNodeId().intValue()).collect(Collectors.toList());
    			
    			if(CollectionUtils.isEmpty(list1)) {
    				log.info("空对象：nodeId:"+acNodeUser.getNodeId());
    			}
    			if(CollectionUtils.isNotEmpty(list1)){
    				acNodeUserVO.setAcCourseNode(list1.get(0));
    			}
    			if(acNodeUserVO.getAcCourseNode().getNodeCode().equals("1")) {
    				firstAcNodeUserVO = acNodeUserVO;
    			}
    			voList.add(acNodeUserVO);
			}
//     	   需要找出最后一个是1的环节 学习状态为1 id最大的
//     		accourseuser/fullList 看课程是否看完
//    		记录课程最后环节
    		QueryWrapper<AcStatisticsUser> qw2 = new QueryWrapper<AcStatisticsUser>();
    		qw2.lambda().eq(AcStatisticsUser::getUserId, userId);
    		qw2.lambda().eq(AcStatisticsUser::getCourseId, courseId);
    		qw2.lambda().eq(AcStatisticsUser::getCourseFinish, 0);
    		AcStatisticsUser acStatisticsUser = acStatisticsUserService.getOne(qw2);
    		if(acStatisticsUser != null ) {
    			String lastNodeName = "";
    			Integer lastNodeId = 0;
    			List<AcNodeUser>list1 = acNodeUserList.stream().filter(x -> x.getStudyStatus() == 1).collect(Collectors.toList());
				if (CollectionUtils.isNotEmpty(list1)) {
					AcNodeUser acnodeUser = list1.stream().max(Comparator.comparing(AcNodeUser::getNodeId)).get();
					List<AcCourseNode> list2 = list.stream()
							.filter(x -> x.getId().intValue() == acnodeUser.getNodeId().intValue())
							.collect(Collectors.toList());
					if (CollectionUtils.isNotEmpty(list2)) {
						acStatisticsUser.setLastNodeName(list2.get(0).getNodeName());
						lastNodeId = acnodeUser.getNodeId();
						lastNodeName = "课程编号："+list2.get(0).getCourseId()+" 环节编号："+lastNodeId+" 环节名称："+list2.get(0).getNodeName()+" 环节内容："+list2.get(0).getNodeContent();
					}
				} else {
					AcNodeUser acnodeUser = acNodeUserList.stream().min(Comparator.comparing(AcNodeUser::getNodeId))
							.get();
					List<AcCourseNode> list2 = list.stream()
							.filter(x -> x.getId().intValue() == acnodeUser.getNodeId().intValue())
							.collect(Collectors.toList());
					lastNodeId = acnodeUser.getNodeId();
					lastNodeName = "课程编号："+list2.get(0).getCourseId()+" 环节编号："+lastNodeId+" 环节名称："+list2.get(0).getNodeName()+" 环节内容："+list2.get(0).getNodeContent();
				}
    			if(acCourseUser.getStatus() == 0) {
//        		找到用户的最后学习环节并记录
					acStatisticsUser.setLastNodeId(lastNodeId);
	    			 acStatisticsUser.setLastNodeName(lastNodeName);
	    			 acStatisticsUser.setMobile(mobile);
	    			 acStatisticsUserService.updateById(acStatisticsUser);
    			}else {
//    				查找大于0并且状态是未完成的记录
    				QueryWrapper<AcStatisticsUser> AcStatisticsUserQueryWrapper3 = new QueryWrapper<AcStatisticsUser>();
    				AcStatisticsUserQueryWrapper3.lambda().eq(AcStatisticsUser::getUserId, userId);
    				AcStatisticsUserQueryWrapper3.lambda().eq(AcStatisticsUser::getCourseId, courseId);
    				AcStatisticsUserQueryWrapper3.lambda().gt(AcStatisticsUser::getRevisitTimes, 0);
    				AcStatisticsUserQueryWrapper3.lambda().eq(AcStatisticsUser::getCourseFinish, 0);
    	    		AcStatisticsUser acStatisticsUser3 = acStatisticsUserService.getOne(AcStatisticsUserQueryWrapper3);
    	    		if(acStatisticsUser3!=null) {
    	    			acStatisticsUser3.setLastNodeId(lastNodeId);
    	       			acStatisticsUser3.setLastNodeName(lastNodeName);
    	       			acStatisticsUser3.setMobile(mobile);
    	       			acStatisticsUserService.updateById(acStatisticsUser3);
    	    		}
    	    	
    			}
    			Long time3 = System.currentTimeMillis();
    			log.info("getListByCourseId 用户统计--------------，用时："+(System.currentTimeMillis()-time3));
    		}
    	}else {
    		QueryWrapper< AcCourseNode> qw2 = new QueryWrapper<>();
    		qw2.lambda().eq(AcCourseNode::getCourseId, courseId);
    		qw2.lambda().orderByAsc(AcCourseNode::getId);
    		List<AcCourseNode> courseNode = acCourseNodeService.list(qw2);
    		for (AcCourseNode acCourseNode : courseNode) {
    			AcNodeUser acNodeUser = new AcNodeUser();
    			acNodeUser.setCourseId(courseId);
    			acNodeUser.setUserId(userId);
    			acNodeUser.setStudyStatus(0);
    			acNodeUser.setNodeId(acCourseNode.getId());
    			acNodeUser.setNodeCode(acCourseNode.getNodeCode());
    			acNodeUser.setIsEnter(0);
    			acNodeUser.setIsQuit(0);
    			acNodeUserList.add(acNodeUser);
    			
    			AcNodeUserVO  acNodeUserVO = mapperFacade.map(acNodeUser, AcNodeUserVO.class);
    			acNodeUserVO.setAcCourseNode(acCourseNode);
    			if(acCourseNode.getNodeCode() .equals("1")) {
    				firstAcNodeUserVO = acNodeUserVO;
    			}
    			voList.add(acNodeUserVO);
			}
    		Long time5 = System.currentTimeMillis();
    		acNodeUserService.saveBatch(acNodeUserList);
    		log.info("getListByCourseId 用户课程节点不存在，保存用户课程节点--------------，用时："+(System.currentTimeMillis()-time5));
    	}
    	if(firstAcNodeUserVO != null && firstAcNodeUserVO.getStudyStatus() == 0) {
    		if(firstAcNodeUserVO.getIsEnter() == 0) {
    			AcNodeUser acNodeUser = 	mapperFacade.map(firstAcNodeUserVO, AcNodeUser.class);
    			acNodeUser.setIsEnter(1);
    			int updateCount = acNodeUserService.changeIsEnter(acNodeUser);
    			if(updateCount == 1) {
    				updateAcStatisticsCourseDetail( firstAcNodeUserVO.getAcCourseNode() );
    			}
    			
    		}
    		Long time6 = System.currentTimeMillis();
    		log.info("getListByCourseId 用户课程节点不存在，记录统计人数--------------，用时："+(System.currentTimeMillis()-time6));
    	}
//  统计课程进入人数
//    	-----------
    	log.info("getListByCourseId 判定是否统计课程进入人数，acCourseUser：{}",acCourseUser);
    	AcCourse acCourse = acCourseService.getById(courseId);
    	if(acCourseUser == null) {
    		acCourseUser = new AcCourseUser();
    		acCourseUser.setCourseCode(acCourse.getCourseCode());
    		acCourseUser.setCourseId(courseId);
    		acCourseUser.setCourseName(acCourse.getCourseName());
    		acCourseUser.setIsEnter(1);
    		acCourseUser.setStatus(0);
    		acCourseUser.setStudyTime(0);
    		acCourseUser.setUserId(userId);
    		Long dataCount = acCourseUserService.count(qw);
    		if(dataCount  == 0) {
    			acCourseUserService.save(acCourseUser);
    			log.info("getListByCourseId 统计课程进入人数命中，统计课程进入人数开始");
        		updateAcStatisticsCourse(courseId, acCourse.getCourseCode(),acCourseUser.getStatus());
        		log.info("getListByCourseId 统计课程进入人数命中，统计课程进入人数结束");
    		}
    	}
    	else if( acCourseUser.getIsEnter() == 0) {
    			updateAcStatisticsCourse(courseId, acCourse.getCourseCode(),acCourseUser.getStatus());
    			acCourseUser.setIsEnter(1);
    		acCourseUserService.updateById(acCourseUser);
    	}
    	Long time7 = System.currentTimeMillis();
    	log.info("getListByCourseId 用户课程节点不存在，处理是否进入--------------，用时："+(System.currentTimeMillis()-time7));
    	result.setData(voList);
    	log.info("getListByCourseId 结束--------------，用时："+(System.currentTimeMillis()-startTime));
    	return result;
	}
	

	/**
	 * 
	 * @param courseCode
	 * @param status 
	 * @param singleCourseFinishRate 单课完成率
	 * @param finishCourseAvageTime 完课平均时长
	 * @param finishCourseMedianTime 完课中位数时长
	 * @param reviewCount 重看人数
	 */
	private void updateAcStatisticsCourse(Integer courseId,int courseCode, Integer status) {
//		Long finishCourseCount = status == 1?1l:0;
		QueryWrapper<AcStatisticsCourse> qw = new QueryWrapper<>();
		qw.lambda().eq(AcStatisticsCourse::getCourseId, courseId);
		AcStatisticsCourse acStatisticsCourse = acStatisticsCourseService.getOne(qw);
//		追加逻辑，如果是已存在，需要重新统计单课完成率 平均时长和完课中位数时长
		if(acStatisticsCourse == null) {
			acStatisticsCourse = new AcStatisticsCourse();
			acStatisticsCourse.setCourseCode(courseCode);
			acStatisticsCourse.setCourseId(courseId);
			acStatisticsCourse.setCourseCode(courseCode);
			acStatisticsCourse.setEnterCourseCount(1l);
			acStatisticsCourseService.save(acStatisticsCourse);
		}
		else {
//		   解决进入课程人数被重复计算的问题，统计课程用户表中同一课程的数量
			acStatisticsCourse.setEnterCourseCount(acStatisticsCourse.getEnterCourseCount()+1);
//			统计单课完成率
			BigDecimal  singleCourseFinishRate = BigDecimal.valueOf(acStatisticsCourse.getFinishCourseCount())
					.divide(BigDecimal.valueOf(acStatisticsCourse.getEnterCourseCount()), 4, RoundingMode.HALF_UP)
					.multiply(BigDecimal.valueOf(100)).setScale(2);
			acStatisticsCourse.setSingleCourseFinishRate(singleCourseFinishRate);
			acStatisticsCourseService.updateByVersion(acStatisticsCourse);
		}
	}
	/**
	 * 修改环节状态并统计进入人数和退出人数
	 * @param nodeId 环节id
	 * @param userId 用户id
	 * @param courseId 课程id
	 * @param Status 状态
	 * @param nextNodeId 下一个环节id， 为空则不需要统计进入人数
	 * @return
	 */
	@Transactional
	public HttpJsonResult<Boolean> changeNodeStatus(Integer nodeId, Long userId,Integer courseId,Integer status, Integer nextNodeId) {
		log.info("修改环节状态,changeNodeStatus被调用，入参：nodeId:{},userId:{},courseId:{},nextNodeId:{}",nodeId,userId,courseId,nextNodeId);
		HttpJsonResult<Boolean>  result = new HttpJsonResult<Boolean> ();
		QueryWrapper< AcNodeUser> qw1 = new QueryWrapper<>();
    	qw1.lambda().eq(AcNodeUser::getNodeId, nodeId);
    	qw1.lambda().eq(AcNodeUser::getUserId, userId);
    	qw1.lambda().eq(AcNodeUser::getCourseId, courseId);
		AcNodeUser acNodeUser = acNodeUserService.getOne(qw1);
		if(acNodeUser == null) {
			result.setMsg("该用户未找到环节信息，请确认数据是否存在");
			return result;
		}
		if(status == acNodeUser.getStudyStatus()) {
			result.setData(true);
			return result;
		}
		acNodeUser.setStudyStatus(status);
//		记录当前学习环节，保存到用户统计表
		AcNodeUser acNodeUser1 = acNodeUserService.getCurrentNode(courseId,userId);
		AcCourseNode acCourseNode = acCourseNodeService.getById(acNodeUser1.getNodeId());
		acNodeUserService.updateById(acNodeUser);
		updateCurrentNode(userId,courseId, acCourseNode.getId(),acCourseNode.getNodeName());
//		统计下一个环节的进入人数
		if(nextNodeId!= null&& nextNodeId>0) {
			QueryWrapper< AcStatisticsCourseDetail> qw3 = new QueryWrapper< AcStatisticsCourseDetail> ();
			qw3.lambda().eq(AcStatisticsCourseDetail::getNodeId, nextNodeId);
			qw3.lambda().eq(AcStatisticsCourseDetail::getCourseId, courseId);
			AcStatisticsCourseDetail nextAcStatisticsCourseDetai  = acStatisticsCourseDetailService.getOne(qw3);
			AcCourseNode nextAcCourseNode = 	acCourseNodeService.getById(nextNodeId);
			QueryWrapper< AcNodeUser> qw4 = new QueryWrapper<>();
	    	qw4.lambda().eq(AcNodeUser::getNodeId, nextNodeId);
	    	qw4.lambda().eq(AcNodeUser::getUserId, userId);
	    	qw4.lambda().eq(AcNodeUser::getCourseId, courseId);
			AcNodeUser nextAcNodeUser = acNodeUserService.getOne(qw4);
			if(nextAcNodeUser.getIsEnter() == 0) {
				nextAcNodeUser.setIsEnter(1);
				int updateCount = acNodeUserService.changeIsEnter(nextAcNodeUser);
				if(updateCount == 1) {
					if(nextAcStatisticsCourseDetai == null) {
						nextAcStatisticsCourseDetai = new AcStatisticsCourseDetail();
						nextAcStatisticsCourseDetai.setNodeId(nextAcCourseNode.getId());
						nextAcStatisticsCourseDetai.setCourseId(nextAcCourseNode.getCourseId());
						nextAcStatisticsCourseDetai.setNodeName(nextAcCourseNode.getNodeName());
						nextAcStatisticsCourseDetai.setNodeContent(nextAcCourseNode.getNodeContent());
						nextAcStatisticsCourseDetai.setNodeCode(nextAcCourseNode.getNodeCode());
						nextAcStatisticsCourseDetai.setEnterCount(1);
					}else {
//						获取下一个环节的进入状态，如果已经进入，不再统计
								nextAcStatisticsCourseDetai.setEnterCount(nextAcStatisticsCourseDetai.getEnterCount()+1);
					}
					acStatisticsCourseDetailService.saveOrUpdate(nextAcStatisticsCourseDetai);
				}
			}
		}
		log.info("修改环节状态,changeNodeStatus方法，调用结束");
		result.setData(true);
		return  result;
	}
	private void updateAcStatisticsCourseDetail(AcCourseNode acCourseNode ) {
		QueryWrapper< AcStatisticsCourseDetail> qw2 = new QueryWrapper< AcStatisticsCourseDetail> ();
		qw2.lambda().eq(AcStatisticsCourseDetail::getNodeId, acCourseNode.getId());
		qw2.lambda().eq(AcStatisticsCourseDetail::getCourseId, acCourseNode.getCourseId());
		AcStatisticsCourseDetail acStatisticsCourseDetai = acStatisticsCourseDetailService.getOne(qw2);
		if(acStatisticsCourseDetai == null) {
			acStatisticsCourseDetai = new AcStatisticsCourseDetail();
			acStatisticsCourseDetai.setNodeId(acCourseNode.getId());
			acStatisticsCourseDetai.setCourseId(acCourseNode.getCourseId());
			acStatisticsCourseDetai.setNodeName(acCourseNode.getNodeName());
			acStatisticsCourseDetai.setNodeContent(acCourseNode.getNodeContent());
			acStatisticsCourseDetai.setNodeCode(acCourseNode.getNodeCode());
			acStatisticsCourseDetai.setEnterCount(1);
		}else {
			acStatisticsCourseDetai.setEnterCount(acStatisticsCourseDetai.getEnterCount() + 1);
		}
		log.info("第一个环节进入人数：{},节点信息：{}",acStatisticsCourseDetai.getEnterCount(),acCourseNode);
		
		acStatisticsCourseDetailService.saveOrUpdate(acStatisticsCourseDetai);
	}
	public HttpJsonResult<AcNodeUserVO> getNodeById(Integer nodeId, Long userId,int courseId) {
		log.info("根据环节id获取环节,getNodeById方法，入参,nodeId:{},userId:{},courseId:{}",nodeId,userId,courseId);
		HttpJsonResult<AcNodeUserVO>  result = new HttpJsonResult<AcNodeUserVO> ();
		QueryWrapper< AcCourseNode> qw = new QueryWrapper<>();
		qw.lambda().eq(AcCourseNode::getId, nodeId);
		qw.lambda().eq(AcCourseNode::getCourseId, courseId);
		AcCourseNode  acCourseNode = 	acCourseNodeService.getOne(qw);
		if(acCourseNode == null) {
			result.setMsg("不存在环节id为:"+nodeId+"课程id为"+courseId+"的环节数据");
			return result;
		}
		QueryWrapper< AcNodeUser> qw1 = new QueryWrapper<>();
    	qw1.lambda().eq(AcNodeUser::getNodeId, nodeId);
    	qw1.lambda().eq(AcNodeUser::getUserId, userId);
    	qw1.lambda().eq(AcNodeUser::getCourseId, courseId);
    	AcNodeUser acNodeUser = acNodeUserService.getOne(qw1);
    	boolean isUpdate = true;
		if(acNodeUser == null) {
			isUpdate = false;
			acNodeUser = new AcNodeUser();
		}
		acNodeUser.setNodeId(nodeId);
		acNodeUser.setUserId(userId);
		acNodeUser.setCourseId(courseId);
		acNodeUser.setNodeCode(acCourseNode.getNodeCode());
		acNodeUser.setStudyStatus(0);
		acNodeUser.setCourseId(acCourseNode.getCourseId());
		acNodeUser.setNodeId(acCourseNode.getId());
//		入库
		if(isUpdate) {
			acNodeUserService.updateById(acNodeUser);
		}else {
//			数据统计
		
			acNodeUserService.save(acNodeUser);
		}
	
		AcNodeUserVO acNodeUserVO = mapperFacade.map(acNodeUser, AcNodeUserVO.class);
		acNodeUserVO.setAcCourseNode(acCourseNode);
		result.setData(acNodeUserVO);
		log.info("根据环节id获取环节,getNodeById方法，调用结束");
		return result;
	}
//	产生重看记录
	public HttpJsonResult<Boolean> changeAllNodeStatus(Long userId, Integer courseId, Integer studyStatus,String mobile) {
		log.info("修改所有节点状态方法changeAllNodeStatus被调用：userId:{},courseId:{},studyStatus:{},mobile:{}",userId,courseId,studyStatus,mobile);
		HttpJsonResult<Boolean>  result = new HttpJsonResult<Boolean> ();
//       重看人数累加
		QueryWrapper<AcCourseUser> acCourseUserQw = new QueryWrapper<AcCourseUser>();
		acCourseUserQw.lambda().eq(AcCourseUser::getCourseId, courseId);
		acCourseUserQw.lambda().eq(AcCourseUser::getUserId, userId);
		AcCourseUser acCourseUser = acCourseUserService.getOne(acCourseUserQw);
		if (acCourseUser != null && acCourseUser.getStatus() == 1) {
//			 从数据库获取统计记录重看次数最大的记录，
			QueryWrapper<AcStatisticsCourse> qw = new QueryWrapper<>();
			qw.lambda().eq(AcStatisticsCourse::getCourseId, courseId);
			AcStatisticsCourse acStatisticsCourse = acStatisticsCourseService.getOne(qw);
// 		增加重看人数
			if (acStatisticsCourse != null) {
				QueryWrapper<AcStatisticsUser> acStatisticsUserQueryWrapper = new QueryWrapper<AcStatisticsUser>();
				acStatisticsUserQueryWrapper.lambda().eq(AcStatisticsUser::getUserId, userId);
				acStatisticsUserQueryWrapper.lambda().eq(AcStatisticsUser::getCourseId, courseId);
				acStatisticsUserQueryWrapper.lambda().eq(AcStatisticsUser::getCourseFinish, 0);
//				为了防止有多个未完课的记录
				if (acStatisticsUserService.count(acStatisticsUserQueryWrapper) == 0) {
					Integer revsitTimes = acStatisticsUserService.getMaxRevisitTimes(userId, courseId);
//					 重看次数+1作为当前重看记录的重看次数
					AcStatisticsUser acStatisticsUser = new AcStatisticsUser();
					acStatisticsUser.setCourseId(courseId);
					acStatisticsUser.setUserId(userId);
					acStatisticsUser.setRevisitTimes(revsitTimes);
					acStatisticsUser.setCourseFinish(0);
					acStatisticsUser.setCourseCode(acCourseUser.getCourseCode());
					acStatisticsUser.setMobile(mobile);
					acStatisticsUserService.save(acStatisticsUser);
					acStatisticsCourse.setReviewCount(acStatisticsCourse.getReviewCount() + 1);
					acStatisticsCourseService.updateById(acStatisticsCourse);
				}
			}
		}
			QueryWrapper<AcNodeUser> qw1 = new QueryWrapper<>();
			qw1.lambda().eq(AcNodeUser::getUserId, userId);
			if (courseId != null) {
				qw1.lambda().eq(AcNodeUser::getCourseId, courseId);
			}
			List<AcNodeUser> acNodeUsers = acNodeUserService.list(qw1);
			List<Integer> nodeIds = new ArrayList<Integer>();
			for (AcNodeUser acNodeUser : acNodeUsers) {
				acNodeUser.setStudyStatus(studyStatus);
				acNodeUserService.updateById(acNodeUser);
//    		数据统计
				nodeIds.add(acNodeUser.getNodeId());
			}
			result.setData(true);
			log.info("修改所有节点状态方法changeAllNodeStatus,调用结束");
		return result;
	}
	
	public void updateCurrentNode(Long userId, int courseId, int nodeId,String nodeName) {
		//此处不能记录最后环节
		QueryWrapper<AcStatisticsUser> qw = new QueryWrapper<AcStatisticsUser>();
		qw.lambda().eq(AcStatisticsUser::getUserId, userId);
		qw.lambda().eq(AcStatisticsUser::getCourseId, courseId);
		qw.lambda().eq(AcStatisticsUser::getCourseFinish, 0);
		AcStatisticsUser acStatisticsUser = 	acStatisticsUserService.getOne(qw);
		log.info("changeNodeStatus方法统计用户信息修改前:{}",acStatisticsUser);
		boolean isUpdate = true;
		if(acStatisticsUser == null) {
			AcCourse acCourse = acCourseService.getById(courseId);
			acStatisticsUser = new  AcStatisticsUser();
			acStatisticsUser.setUserId(userId);
			acStatisticsUser.setCourseCode(acCourse.getCourseCode());
			acStatisticsUser.setCourseId(courseId);
			isUpdate = false;
		}
		SysUser user = sysUserService.getById(userId);
		String mobile = user.getMobile();
		if(StringUtils.isBlank(mobile)) {
			mobile = user.getUsername();
		}
		acStatisticsUser.setMobile(mobile);
		log.info("changeNodeStatus方法统计用户信息修改后:{}",acStatisticsUser);
		if(isUpdate ) {
			acStatisticsUserService.updateById(acStatisticsUser);
		}else {
			acStatisticsUserService.save(acStatisticsUser);
		}
		
	}
	public static void main(String[] args) {
		List<AcNodeUser> a = new ArrayList<AcNodeUser>();
		for (int i = 0;i<100;i++) {
			AcNodeUser acNodeUser =new AcNodeUser();
			acNodeUser.setId(i+1);
			if(i %2 ==1) {
				acNodeUser.setStudyStatus(1);
			}else {
				acNodeUser.setStudyStatus(0);
				
			}
			a.add(acNodeUser);
			
		}
		
		Optional<AcNodeUser> user =		a.stream().filter(x->x.getStudyStatus() == 1).max(Comparator.comparing(AcNodeUser::getNodeId));
		System.out.println(user.get().getId());
	}
}
